package com.ihelin.insurance.interceptor;

import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AdminUserInterceptor implements HandlerInterceptor {

//    public static final ThreadLocal<MemberRsepVo> THREAD_LOCAL = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        if (antPathMatcher.match("/admin/login", uri)) {
            return true;
        }
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("adminUser");
        if (loginUser == null) {
            // 没登录就去登录
            response.sendRedirect("/admin/login");
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        THREAD_LOCAL.remove();
    }
}
